package com.mdd.admin.service.impl;

import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mdd.admin.service.IAppVersionService;
import com.mdd.admin.validate.AppVersionCreateValidate;
import com.mdd.admin.validate.AppVersionSearchValidate;
import com.mdd.admin.validate.AppVersionUpdateValidate;
import com.mdd.admin.validate.commons.PageValidate;
import com.mdd.admin.vo.AppVersionDetailVo;
import com.mdd.admin.vo.AppVersionListedVo;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.AppVersion;
import com.mdd.common.mapper.AppVersionMapper;
import com.mdd.common.util.TimeUtils;

/**
 * APP版本更新实现类
 * @author DEREK
 */
@Service
public class AppVersionServiceImpl implements IAppVersionService {
        
    @Resource
    AppVersionMapper appVersionMapper;

    /**
     * APP版本更新列表
     *
     * @author DEREK
     * @param pageValidate 分页参数
     * @param searchValidate 搜索参数
     * @return PageResult<AppVersionListedVo>
     */
    @Override
    public PageResult<AppVersionListedVo> list(PageValidate pageValidate, AppVersionSearchValidate searchValidate) {
        Integer page  = pageValidate.getPageNo();
        Integer limit = pageValidate.getPageSize();

        QueryWrapper<AppVersion> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");

        appVersionMapper.setSearch(queryWrapper, searchValidate, new String[]{
            "=:clientType@client_type:int",
            "=:versionCode@version_code:int",
            "like:versionName@version_name:str",
            "=:path:str",
            "=:content:str",
            "=:isForce@is_force:int",
            "=:updtaeTime@updtae_time:str",
        });

        IPage<AppVersion> iPage = appVersionMapper.selectPage(new Page<>(page, limit), queryWrapper);

        List<AppVersionListedVo> list = new LinkedList<>();
        for(AppVersion item : iPage.getRecords()) {
            AppVersionListedVo vo = new AppVersionListedVo();
            BeanUtils.copyProperties(item, vo);
            vo.setCreateTime(TimeUtils.parseDateToStr("yyyy-MM-dd HH:mm:ss",item.getCreateTime()));
            if(null != item.getUpdtaeTime()) {
            	vo.setUpdtaeTime(TimeUtils.parseDateToStr("yyyy-MM-dd HH:mm:ss",item.getUpdtaeTime()));
            }
            list.add(vo);
        }
        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
    }

    /**
     * APP版本更新详情
     *
     * @author DEREK
     * @param id 主键参数
     * @return AppVersion
     */
    @Override
    public AppVersionDetailVo detail(Integer id) {
        AppVersion model = appVersionMapper.selectOne(
                new QueryWrapper<AppVersion>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在");

        AppVersionDetailVo vo = new AppVersionDetailVo();
        BeanUtils.copyProperties(model, vo);
        return vo;
    }

    /**
     * APP版本更新新增
     *
     * @author DEREK
     * @param createValidate 参数
     */
    @Override
    public void add(AppVersionCreateValidate createValidate) {
        AppVersion model = new AppVersion();
        model.setClientType(createValidate.getClientType());
        model.setVersionCode(createValidate.getVersionCode());
        model.setVersionName(createValidate.getVersionName());
        model.setPath(createValidate.getPath());
        model.setContent(createValidate.getContent());
        model.setIsForce(createValidate.getIsForce());
        model.setCreateTime(new Date());
        model.setUpdtaeTime(new Date());
        appVersionMapper.insert(model);
    }

    /**
     * APP版本更新编辑
     *
     * @author DEREK
     * @param updateValidate 参数
     */
    @Override
    public void edit(AppVersionUpdateValidate updateValidate) {
        AppVersion model = appVersionMapper.selectOne(
                new QueryWrapper<AppVersion>()
                    .eq("id",  updateValidate.getId())
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setId(updateValidate.getId());
        model.setClientType(updateValidate.getClientType());
        model.setVersionCode(updateValidate.getVersionCode());
        model.setVersionName(updateValidate.getVersionName());
        model.setPath(updateValidate.getPath());
        model.setContent(updateValidate.getContent());
        model.setIsForce(updateValidate.getIsForce());
        model.setUpdtaeTime(new Date());
        appVersionMapper.updateById(model);
    }

    /**
     * APP版本更新删除
     *
     * @author DEREK
     * @param id 主键ID
     */
    @Override
    public void del(Integer id) {
        AppVersion model = appVersionMapper.selectOne(
                new QueryWrapper<AppVersion>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        appVersionMapper.delete(new QueryWrapper<AppVersion>().eq("id", id));
    }

}
